package com.bfsuolframework.security.controllar;

import com.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuolframework.core.controller.support.Pagination;
import com.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuolframework.core.spring.QueryParameter;
import com.bfsuolframework.security.annotations.*;
import com.bfsuolframework.security.entity.Privilege;
import com.bfsuolframework.security.entity.RolePrivilege;
import com.bfsuolframework.security.service._PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by andy on 2020/10/19.
 */


@Controller
@RequestMapping("/components/security/privilege")
public class _PrivilegeController extends SpringControllerSupport {

    @Autowired
    private _PrivilegeService privilegeService;

    @SecPrivilege(title = "权限设置")
    @RequestMapping("/index")
    public String index(Model model){
        return getViewNamedPath("index");
    }

    @SecList
    @RequestMapping("/list")
    public String list(
            @QueryParameter("query") QueryParam query,
            @ModelAttribute("page") Pagination page){

        page = privilegeService.getPage(query, page);

        return getViewNamedPath("list");
    }

    @RequestMapping("/input")
    public String create(@ModelAttribute RolePrivilege rolePrivilege, Model model){
        return getViewNamedPath("input");
    }

    @RequestMapping("/input/{id}")
    public String edit(@PathVariable("id") Long id, Model model){
        Privilege privilege = privilegeService.getById(id);
        model.addAttribute(privilege);
        return getViewNamedPath("input");
    }

    @SecCreate
    @SecEdit
    @RequestMapping("/save")
    public @ResponseBody
    MessageJson save(@ModelAttribute Privilege privilege){
        if(privilege.isNew()){
            privilegeService.save(privilege);
        }else{
            privilegeService.update(privilege);
        }
        return MessageJson.information("保存成功");
    }

}
